package com.zuxia.controller;


import com.zuxia.entity.JwtInfo;
import com.zuxia.entity.Orders;
import com.zuxia.exception.ZuxiaBusinessException;
import com.zuxia.param.OrderNextParam;
import com.zuxia.param.OrderPageParam;
import com.zuxia.param.OrderParam;
import com.zuxia.service.OrderService;
import com.zuxia.utils.JwtUtils;
import com.zuxia.utils.R;
import com.zuxia.utils.ResultCodeEnum;
import com.zuxia.utils.TimeUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author laoli
 * @since 2024-11-18
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private OrderService orderService;

    @ApiOperation("支付完成-支付宝回调")
    @GetMapping("/submit")
    public R submitOrder(HttpServletRequest request) throws Exception{
        Map<String, String[]> parameterMap = request.getParameterMap();
        if(parameterMap!=null){
            String tradeStatus = parameterMap.get("trade_status")[0];
            if("TRADE_SUCCESS".equals(tradeStatus)){ //支付宝异步回调 支付成功
                //修改订单状态
                String trade = parameterMap.get("out_trade_no")[0];
                String pagTime = parameterMap.get("gmt_payment")[0];
                Orders orders = new Orders();
                orders.setOrdersStatus(1);
                orders.setOrdersPaymentTime(pagTime);
                orders.setOrdersId(trade);

                return R.ok().message("支付成功").data("data", orderService.submit(orders));
            }else {
                throw new ZuxiaBusinessException(ResultCodeEnum.ORDERS_EROEE);
            }
        }else {
            throw new ZuxiaBusinessException(ResultCodeEnum.ORDERS_EROEE);
        }
    }

    @ApiOperation("提交订单-测试")
    @GetMapping("/submit/test/{id}")
    public R submitTestOrder(@PathVariable("id") String id) throws Exception{
        Orders orders = new Orders();
        orders.setOrdersStatus(1);
        orders.setOrdersPaymentTime(TimeUtils.getY_M_D_H_M_S(System.currentTimeMillis()));
        orders.setOrdersId(id);
        return R.ok().message("支付成功").data("data", orderService.submit(orders));
    }

    @ApiOperation("提交订单-返回表单")
    @GetMapping("/submit/{id}")
    public R submitOrder(@PathVariable("id") String id) throws Exception{
        return R.ok().message("支付成功").data("data", orderService.payment(id));
    }

    @ApiOperation("查询该拼团的人数 --0、1")
    @GetMapping("/group/count/{id}")
    public R getCountMemberByOrderId(@PathVariable("id") String id) throws Exception{
        return R.ok().message("人数").data("order-group-count", orderService.getCountMemberByOrderId(id));
    }

    @ApiOperation("根据条件查询创建了拼团的信息")
    @PostMapping("/group/page")
    public R getOrderPageList(@RequestBody OrderPageParam param) throws Exception{
        return R.ok().message("拼团信息").data("order-group-page", orderService.getOrderPageList(param));
    }

    @ApiOperation("查询拼团-单查(简)")
    @GetMapping("/group/single/{id}")
    public R getOrderById(@PathVariable("id") String id) throws Exception{
        return R.ok().message("拼团信息").data("order-group-single", orderService.getOrderById(id));
    }

    @ApiOperation("查询拼团-单查(详)")
    @GetMapping("/group/single/orders/{id}/{status}")
    public R getOrderOnOrdersById(@PathVariable("id") String id,@PathVariable("status") Integer status) throws Exception{
        return R.ok().message("拼团信息").data("order-group-single-orders", orderService.getOrderOnOrdersById(id,status));
    }

    @ApiOperation("查询指定订单详情信息")
    @GetMapping("/single/orders/{id}")
    public R getOrdersById(@PathVariable("id") String id) throws Exception{
        return R.ok().message("订单信息").data("order-single-orders", orderService.getOrdersById(id));
    }

    @ApiOperation("取消订单")
    @GetMapping("/up/orders/status/cancel/{id}")
    public R cancelOrdersById(@PathVariable("id") String id) throws Exception{
        return R.ok().message("订单以取消").data("orders-status-cancel", orderService.upOrdersStatusById(id,2));
    }

    @ApiOperation("根据订单id查询拼团编号")
    @GetMapping("/orderId/{id}")
    public R selectOrderIdById(@PathVariable("id") String id) throws Exception{
        return R.ok().message("拼团编号").data("orderId", orderService.selectOrderIdById(id));
    }

    @RestController
    @RequestMapping("/order/token")
    class Token{
        @ApiOperation("用户拼团-首拼-创建拼团")
        @PostMapping("/take/star")
        public R partStarTakeOrder(@RequestBody OrderParam orderParam) throws Exception{
            return R.ok().message("拼团成功").data("data", orderService.partStarTakeOrder(orderParam));
        }

        @ApiOperation("用户拼团-后拼-加入拼团")
        @PostMapping("/take/next")
        public R partNextTakeOrder(@RequestBody OrderNextParam orderParam) throws Exception{
            return R.ok().message("拼团成功").data("data", orderService.partNextTakeOrder(orderParam));
        }

        @ApiOperation("用户分页查询订单")
        @PostMapping("/page/list")
        public R getOrdersByUserIdPageList(@RequestBody OrderPageParam orderParam) throws Exception{
            return R.ok().message("拼团成功").data("orders-user-id", orderService.getOrdersByUserIdPageList(orderParam));
        }
    }

    @RestController
    @RequestMapping("/order/merchant")
    class Merchant{
        @ApiOperation("商家发货-修改状态-6")
        @GetMapping("/send/shop/{id}")
        public R sendShop(@PathVariable("id") String id) throws Exception{
            return R.ok().message("修改成功").data("data", orderService.sendShop(id));
        }
    }
}

